package javagprssrv;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.log4j.Logger;
import org.dusin.commonutil.DateUtil;

public class JGS_Param {

    private static Logger logger = Logger.getLogger(JGS_Param.class.getName());
    public static Properties properties = null;
    public static java.lang.String AppStartTime;
    public static java.lang.String Param_OS_Name;
    public static java.lang.String Param_OS_Version;
    public static java.lang.String Param_Java_Name;
    public static java.lang.String Param_Java_Version;
    public static java.lang.String Param_Java_JRE_Dir;
    public static boolean isSystemReady;
    public static java.lang.String TaskDateTime;
    public static int TaskIndex;
    public static java.lang.String ScriptFileName;
    public static int testInt;
    public static java.lang.String INIFileName;
    public static java.lang.String AppVersion;
    public static java.lang.String AppName;
    public static java.lang.String AutoReset;
    public static java.lang.String AutoReset_Time;
    public static java.lang.String LogFileName;
    public static javagprssrv.Log myLog;
    public static final java.util.Vector ClientPipe;
    public static final java.util.Vector UDPServerSockets;
    public static final java.util.Vector TCPServerSockets;
    public static final java.util.Vector JSSJClients;
    public static final java.util.Vector UnDoneRecvDatas;
    public static final java.util.Vector UnTransmitRecvDatas;
    public static final java.util.Vector UnDoneDBActionRecvDatas;
    public static final java.util.Vector IATasks;
    public static final java.util.Vector<java.lang.String> UnSavedDBSQLString;
    public static final Set<String> flagSetFor5m;//5m种标记结合,表示某条数据是否插入过数据库。如果插入过，就不用再次插入
    public static final Set<String> heartBeatSet;//心跳数据，所有心跳数据将先加入该结合，然后再进行更新数据库
    public static final LinkedBlockingQueue<Object[]> queueFor5m;   //5分钟插入队列
    public static final LinkedBlockingQueue<Object[]> queueForEach5m;   //5分钟逐条插入队列
    public static int countSavedSQL_Success;
    public static int countSavedSQL_Failure;
    public static final java.util.Vector<javagprssrv.StructClass.structRecvData> UnSavedRecvData_UDP;
    public static final java.util.Vector<javagprssrv.StructClass.structRecvData> UnSavedRecvData_TCP;
    public static javagprssrv.StructClass.structOutputSocket OutputSocket;

    public static final java.util.Vector<javagprssrv.StructClass.structCOMMTerm> COMMTerms;
    public static final java.util.Vector UnDoneCOMMTasks;
    public static int TaskCount;
    public static boolean isServerShowLog;
    public static final java.util.Vector<javagprssrv.StructClass.structTerm> Terms;
    public static final java.util.Vector<javagprssrv.StructClass.structTerm> TCP_Terms;
    public static final java.util.Vector<javagprssrv.StructClass.structTask> Tasks;
    public static final java.util.Vector<javagprssrv.StructClass.structTask> TempTasks;
    public static int countDataAnalyse_DBAction_BusyThread;
    public static long countReceiveData_All;
    public static long countSendData_All;
    public static long countReceiveData_TCP;
    public static long countReceiveData_UDP;
    public static long countSendData_TCP;
    public static long countSendData_UDP;
    public static long countSendData_Transmit;
    public static long timesReceiveData_All;
    public static long timesReceiveData_TCP;
    public static long timesReceiveData_UDP;
    public static long timesSendData_TCP;
    public static long timesSendData_UDP;
    public static int DB_Counts;
    public static java.util.Vector<javagprssrv.StructClass.structDBInfo> DBInfoS;
    public static java.util.Vector<javagprssrv.DataBase.DBConnectionPool> DBConnectionS;
    public static int _TaskDiapatcherCount;
    public static int _TaskFailedRetryTimes;
    public static int _TaskFailedSeconds;
    public static int _DialSeconds;
    public static int _AfterErrorRetrySeconds;
    public static int _GetDataLogDeleteSeconds;
    public static int _TaskManagerSeconds;
    public static int _TaskAutoGetDateMonth;
    public static int _GetDataBeforeDays;
    public static int _TaskMaxCount;
    public static int _TrdDataAnalyseCount;
    public static int _TrdDataAnalyse_DBAction_Count;
    public static int _CanAddSameJob;

    public static synchronized void setDataAnalyse_DBAction_BusyFlag(final boolean isBusy) {
        int idx = -1;
        if (isBusy) {
            idx = 1;
        } else {
            idx = -1;
        }
        javagprssrv.JGS_Param.countDataAnalyse_DBAction_BusyThread += idx;
    }

    public static java.util.Vector<java.lang.String> getTXTFile(final java.lang.String TXTFileName) {
        final java.util.Vector<java.lang.String> vector = new java.util.Vector<java.lang.String>();
        java.lang.String iniLine = "";
        int countLine = 0;
        java.lang.String strCountLine = "";
        try {
            final java.io.FileReader fr = new java.io.FileReader(TXTFileName);
            final java.io.BufferedReader br = new java.io.BufferedReader(fr);
            while (iniLine != null) {
                iniLine = br.readLine().trim();
                ++countLine;
                if (countLine < 10000) {
                    strCountLine = "" + countLine;
                }
                if (countLine < 1000) {
                    strCountLine = "0" + countLine;
                }
                if (countLine < 100) {
                    strCountLine = "00" + countLine;
                }
                if (countLine < 10) {
                    strCountLine = "000" + countLine;
                }
                iniLine = strCountLine + ":   " + iniLine;
                vector.add(iniLine);
            }
            br.close();
            fr.close();
        } catch (java.lang.Exception ex) {
        }
        return vector;
    }

    static {
        ClientPipe = new java.util.Vector();
        UDPServerSockets = new java.util.Vector();
        TCPServerSockets = new java.util.Vector();
        JSSJClients = new java.util.Vector();
        UnDoneRecvDatas = new java.util.Vector();
        UnTransmitRecvDatas = new java.util.Vector();
        UnDoneDBActionRecvDatas = new java.util.Vector();
        IATasks = new java.util.Vector();
        UnSavedDBSQLString = new java.util.Vector<java.lang.String>();
        queueFor5m = new LinkedBlockingQueue<>();
        queueForEach5m = new LinkedBlockingQueue<>();
        flagSetFor5m = Collections.synchronizedSet(new LinkedHashSet<String>());
        heartBeatSet = Collections.synchronizedSet(new LinkedHashSet<String>());
        UnSavedRecvData_UDP = new java.util.Vector<javagprssrv.StructClass.structRecvData>();
        UnSavedRecvData_TCP = new java.util.Vector<javagprssrv.StructClass.structRecvData>();

        COMMTerms = new java.util.Vector<javagprssrv.StructClass.structCOMMTerm>();
        UnDoneCOMMTasks = new java.util.Vector();

        Terms = new java.util.Vector<javagprssrv.StructClass.structTerm>();
        TCP_Terms = new java.util.Vector<javagprssrv.StructClass.structTerm>();
        Tasks = new java.util.Vector<javagprssrv.StructClass.structTask>();
        TempTasks = new java.util.Vector<javagprssrv.StructClass.structTask>();
    }

    public static void setParams() {
        
        try {
            properties = new Properties();
            // 使用InPutStream流读取properties文件
            BufferedReader bufferedReader = new BufferedReader(new FileReader("GPRS.properties"));
            properties.load(bufferedReader);
        } catch (FileNotFoundException e) {
            logger.error("GPRS.properties配置文件不存在");
        } catch (IOException ex) {
            logger.error("GPRS.peroperties配置文件读写错误");
        }

        javagprssrv.JGS_Param.Param_OS_Name = "";
        javagprssrv.JGS_Param.Param_OS_Version = "";
        javagprssrv.JGS_Param.Param_Java_Name = "";
        javagprssrv.JGS_Param.Param_Java_Version = "";
        javagprssrv.JGS_Param.Param_Java_JRE_Dir = "";
        javagprssrv.JGS_Param.isSystemReady = false;
        javagprssrv.JGS_Param.TaskDateTime = "";
        javagprssrv.JGS_Param.TaskIndex = 0;
        javagprssrv.JGS_Param.ScriptFileName = "script.js";
        javagprssrv.JGS_Param.testInt = 0;
        javagprssrv.JGS_Param.INIFileName = "GPRS.ini";
        javagprssrv.JGS_Param.AppVersion = "J_2.a.6.218";
        javagprssrv.JGS_Param.AppName = "VV2012 网络数据流服务器";
        javagprssrv.JGS_Param.AutoReset = "0";
        javagprssrv.JGS_Param.AutoReset_Time = properties.getProperty( "复位检查时间").trim();
        javagprssrv.JGS_Param.LogFileName = DateUtil.get(new Date(), "yyyyMMdd");
        javagprssrv.JGS_Param.myLog = new javagprssrv.Log();

        javagprssrv.JGS_Param.countSavedSQL_Success = 0;
        javagprssrv.JGS_Param.countSavedSQL_Failure = 0;

        javagprssrv.JGS_Param.TaskCount = 0;
        javagprssrv.JGS_Param.isServerShowLog = true;

        javagprssrv.JGS_Param.countDataAnalyse_DBAction_BusyThread = 0;
        javagprssrv.JGS_Param.countReceiveData_All = 0L;
        javagprssrv.JGS_Param.countSendData_All = 0L;
        javagprssrv.JGS_Param.countReceiveData_TCP = 0L;
        javagprssrv.JGS_Param.countReceiveData_UDP = 0L;
        javagprssrv.JGS_Param.countSendData_TCP = 0L;
        javagprssrv.JGS_Param.countSendData_UDP = 0L;
        javagprssrv.JGS_Param.countSendData_Transmit = 0L;
        javagprssrv.JGS_Param.timesReceiveData_All = 0L;
        javagprssrv.JGS_Param.timesReceiveData_TCP = 0L;
        javagprssrv.JGS_Param.timesReceiveData_UDP = 0L;
        javagprssrv.JGS_Param.timesSendData_TCP = 0L;
        javagprssrv.JGS_Param.timesSendData_UDP = 0L;
        javagprssrv.JGS_Param.DB_Counts = java.lang.Integer.valueOf(properties.getProperty("数据库数量").trim());
        javagprssrv.JGS_Param.DBInfoS = new java.util.Vector<javagprssrv.StructClass.structDBInfo>();
        javagprssrv.JGS_Param.DBConnectionS = new java.util.Vector<javagprssrv.DataBase.DBConnectionPool>();
        javagprssrv.JGS_Param._TaskDiapatcherCount = java.lang.Integer.valueOf(properties.getProperty("调度线程").trim());
        javagprssrv.JGS_Param._TaskFailedRetryTimes = java.lang.Integer.valueOf(properties.getProperty("任务失败次数").trim());
        javagprssrv.JGS_Param._TaskFailedSeconds = java.lang.Integer.valueOf(properties.getProperty("任务失败时间").trim());
        javagprssrv.JGS_Param._DialSeconds = java.lang.Integer.valueOf(properties.getProperty("拨号时间").trim());
        javagprssrv.JGS_Param._AfterErrorRetrySeconds = java.lang.Integer.valueOf(properties.getProperty("失败后重取间隔时间").trim());
        javagprssrv.JGS_Param._GetDataLogDeleteSeconds = java.lang.Integer.valueOf(properties.getProperty("取数记录保存时间").trim());
        javagprssrv.JGS_Param._TaskManagerSeconds = java.lang.Integer.valueOf(properties.getProperty("任务调度时间").trim());
        javagprssrv.JGS_Param._TaskAutoGetDateMonth = java.lang.Integer.valueOf(properties.getProperty("自动取日月数据时间").trim());
        javagprssrv.JGS_Param._GetDataBeforeDays = java.lang.Integer.valueOf(properties.getProperty("取X天前数据").trim());
        javagprssrv.JGS_Param._TaskMaxCount = java.lang.Integer.valueOf(properties.getProperty("最大任务数").trim());
        javagprssrv.JGS_Param._TrdDataAnalyseCount = java.lang.Integer.valueOf(properties.getProperty("数据分析线程数").trim());
        javagprssrv.JGS_Param._TrdDataAnalyse_DBAction_Count = java.lang.Integer.valueOf(properties.getProperty("数据分析(数据库操作)线程数").trim());
        javagprssrv.JGS_Param._CanAddSameJob = java.lang.Integer.valueOf(properties.getProperty( "允许添加相同任务").trim());
    }
}
